package com.cortex.pineapple.activities;


import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebIconDatabase;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.cortex.pineapple.PineAppleApplication;
import com.cortex.pineapple.R;
import com.cortex.pineapple.database.Season;
import com.cortex.pineapple.utilities.ViewServer;
import com.cortex.pineapple.view.BrowserView;
import com.cortex.pineapple.view.BrowserView.OnIconClickListener;
import com.cortex.pineapple.view.HomeView;
import com.cortex.pineapple.view.HomeView.OnLinkSelectedListener;
import com.cortex.pineapple.view.HomeView.OnStartNewSeasonListener;
import com.cortex.pineapple.view.HomeView.STATE_HOME_VIEW;
import com.cortext.pineapple.link.Link;


public class MainActivity extends Activity {

	private HomeView homeView;
	private PineAppleApplication app;
	private BrowserView browserView;
	private ViewFlipper flipperMain;
	private StateActivityViewEnum stateActivity;
	private final static String KEY_CURRENT_VIEW_STATE = "currentViewState";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		ViewServer.get(this).addWindow(this);
		// to allow get favicons from webView
		WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
	}
	@Override
	protected void onResume() {
		super.onResume();
		checkInternetConnection();
		ViewServer.get(this).setFocusedWindow(this);
	}
	 @Override
    public void onDestroy() {
    	super.onDestroy();
    	ViewServer.get(this).removeWindow(this);
    }
	@Override
	protected void onSaveInstanceState(Bundle outState){
		super.onSaveInstanceState(outState);
		homeView.saveState();
		browserView.saveState(outState);
		outState.putInt(KEY_CURRENT_VIEW_STATE, stateActivity.ordinal());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState){
		super.onRestoreInstanceState(savedInstanceState);
		stateActivity = StateActivityViewEnum.values()[savedInstanceState.getInt(KEY_CURRENT_VIEW_STATE)];
		browserView.restoreState(savedInstanceState);
		homeView.restoreState();
		moveToStateActivity(stateActivity);
		
	}
	@Override
	public void onBackPressed() {
		browserView.stopLoding();
		switch(stateActivity){
		case BROWSER:
			moveToStateActivity(StateActivityViewEnum.HOME);
			break;
		case HOME:
		default:
			if(homeView.getStateHomeView() == STATE_HOME_VIEW.HOME){
				super.onBackPressed();
			}
			else{
				homeView.back();
			}
			break;
		}
	}
	private void init(){
		app = (PineAppleApplication)getApplicationContext();
		browserView = (BrowserView)findViewById(R.id.browserView);
		homeView = (HomeView)findViewById(R.id.homeView);
		flipperMain = (ViewFlipper)findViewById(R.id.flipper);
		stateActivity = StateActivityViewEnum.HOME;

		Animation in = AnimationUtils.loadAnimation(this, R.anim.push_right_and_fade_in);
		Animation out = AnimationUtils.loadAnimation(this, R.anim.push_right_and_fade_out);
		flipperMain.setInAnimation(in);
		flipperMain.setOutAnimation(out);
		initListeners();
//		browserView.loadUrl("http://www.ynet.co.il/articles/0,7340,L-4504714,00.html");
//		moveToStateActivity(StateActivityViewEnum.BROWSER);
	}
	private void initListeners(){
		homeView.setOnLinkSelectedListener(new OnLinkSelectedListener() {

			@Override
			public void linkSelected(Link link) {
				browserView.loadUrl(link.getUrl());
				moveToStateActivity(StateActivityViewEnum.BROWSER);
			}
		});
		homeView.setOnStartNewSeasonListener(new OnStartNewSeasonListener() {
			
			@Override
			public void startedNewSeason(Season season) {
				moveToStateActivity(StateActivityViewEnum.HOME);
			}
		});
		browserView.setOnIconClickListener(new OnIconClickListener() {

			@Override
			public void onIconClick() {
				moveToStateActivity(StateActivityViewEnum.HOME);
			}
		});
		browserView.setOnBrowserChangedListener(homeView);
		browserView.setOnSectionSelectedListener(homeView);
	}
	private void moveToStateActivity(StateActivityViewEnum state){
		switch(state){
		case BROWSER:
			if(flipperMain.getChildAt(
					flipperMain.getDisplayedChild()).getId() != R.id.browserView){
				flipperMain.setDisplayedChild(1);
			}
			break;
		case HOME:
			if(flipperMain.getChildAt(
					flipperMain.getDisplayedChild()).getId() != R.id.homeView){
				flipperMain.setDisplayedChild(0);
			}
			break;
		}

		stateActivity = state;
	}
	private boolean checkInternetConnection(){
		if(!app.isInternetConnection()){
			Toast.makeText(this, getResources().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
		}
		return app.isInternetConnection();
	}
	public enum StateActivityViewEnum{
		HOME,
		BROWSER;
	}

}
